package com.xm.transfer.utils;

import com.xm.transfer.annotations.Component;

import java.sql.Connection;
import java.sql.SQLException;

@Component
public class ConnectionUtils {

    private ThreadLocal<Connection> connectionThreadLocal = new ThreadLocal<>();

//    private static ConnectionUtils connectionUtils = new ConnectionUtils();
//
//    public static ConnectionUtils getInstance(){
//        if (null == connectionUtils) {
//            synchronized (ConnectionUtils.class) {
//                if (null == connectionUtils) {
//                    connectionUtils = new ConnectionUtils();
//                }
//            }
//        }
//        return connectionUtils;
//    }

    public Connection getCurrentThreadConnection() throws SQLException {
        if (null == connectionThreadLocal.get()) {
            connectionThreadLocal.set(DruidUtils.getInstance().getConnection());
        }
        return connectionThreadLocal.get();
    }
}
